package cn.com.lookang.robot.service.impl;

import cn.com.lookang.robot.dao.PlayRealmDao;
import cn.com.lookang.robot.dao.RealmPlayerInfoDao;
import cn.com.lookang.robot.entity.PlayRealmEntity;
import cn.com.lookang.robot.entity.RealmPlayerInfoEntity;
import cn.com.lookang.robot.fnlp.Fnlp;
import cn.com.lookang.robot.service.GroupMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author liulekang
 * @date 2020/7/25
 */
@Service
public class GroupMessageServiceImpl implements GroupMessageService {

    @Autowired
    private PlayRealmDao playRealmDao;

    @Autowired
    private RealmPlayerInfoDao realmPlayerInfoDao;

    /**
     * 开团
     *
     * @param realmName
     * @param rootPlayerName
     * @param rootPlayerQq
     * @param playDate
     * @param playerNum
     */
    @Override
    public void playRealm(String realmName, String rootPlayerName, String rootPlayerQq, String playDate, String playerNum) {
        //开团时间需进行时间短语识别
        Date date = Fnlp.parseDate(playDate);
        PlayRealmEntity playRealm = PlayRealmEntity.builder().realmName(realmName).rootPlayerName(rootPlayerName).rootPlayerQq(rootPlayerQq)
                .playerNum(Integer.parseInt(playerNum)).playDate(date).build();
        playRealmDao.save(playRealm);
        //同时保存一个报名人信息
        RealmPlayerInfoEntity realmPlayerInfo = RealmPlayerInfoEntity.builder().playerName(rootPlayerName).playerQq(rootPlayerQq)
                .playRealmId(playRealm.getId()).build();
        realmPlayerInfoDao.save(realmPlayerInfo);
    }

    /**
     * 报名副本
     *
     * @param realmName    副本名称
     * @param playerName   玩家姓名
     * @param playerQq     玩家QQ
     * @param playerCareer 玩家职业
     * @return 该副本已报名人数
     */
    @Override
    public String enrollRealm(String realmName, String playerName, String playerQq, String playerCareer) {
        PlayRealmEntity playRealm;
        if (StringUtils.isNotBlank(realmName)) {
            //根据副本名称查找对应的开团信息
            playRealm = playRealmDao.getPlayRealmByRealmName(realmName);
        } else {
            //查找时间最近的开团信息
            playRealm = playRealmDao.getPlayRealmLast();
        }
        if (playRealm == null) {
            return String.format("报名失败，现在还没有要开的团~");
        }
        int count = realmPlayerInfoDao.countByPlayRealmId(playRealm.getId());
        if (count >= playRealm.getPlayerNum()) {
            return String.format("报名失败，[%s]人数已满", playRealm.getRealmName());
        }
        int countByPlayerQq = realmPlayerInfoDao.countByPlayerQqAndPlayRealmId(playerQq,playRealm.getId());
        if (countByPlayerQq >= 1) {
            return "你已经报名过该副本，不用重新报名";
        }

        RealmPlayerInfoEntity realmPlayerInfo = RealmPlayerInfoEntity.builder().playerName(playerName).playerQq(playerQq)
                .playerCareer(playerCareer).playRealmId(playRealm.getId()).build();
        realmPlayerInfoDao.save(realmPlayerInfo);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(playRealm.getPlayDate().toInstant(), ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd号HH点mm分");
        String dateStr = formatter.format(localDateTime);
        return String.format("报名%s成功，%s发车，目前人数：%d人", playRealm.getRealmName(), dateStr, count + 1);
    }
}
